package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.HttpStatusEnum;
import com.example.common.codeFactory.CodeGenerateStrategy;
import com.example.common.codeFactory.CodeGenerateStrategyFactory;
import com.example.common.codeFactory.entity.CodeGenerateType;
import com.example.entity.*;
import com.example.mapper.EssayTypeMapper;
import com.example.pojo.EssayType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 文章类型表(EssayType)表服务实现类
 *
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/01/17
 * @time: 23:13
 */
@Service
public class EssayTypeService{
    /**
     * mapper对象 
     */
    @Resource
    private EssayTypeMapper essayTypeMapper;
    // 编号工厂
    @Resource
    private CodeGenerateStrategyFactory codeGenerateStrategyFactory;
    
    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param essayType 筛选条件
     * @return
     */
    public ResponseData getByPage(Integer currentPage, Integer pageSize, EssayType essayType) {
        QueryWrapper<EssayType> qw = new QueryWrapper<>();
        //TODO: getByPage essayType qw查询条件待构建
        IPage ipage = new Page(currentPage,pageSize);
        essayTypeMapper.selectPage(ipage,qw);
        //若当前页码大于总页面数
        if (currentPage > ipage.getPages()){
            ipage = new Page(ipage.getPages(),pageSize);
            essayTypeMapper.selectPage(ipage,qw);
        }
        if (ipage.getTotal()<=0){
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(),ipage);
    }
    
    /**
     * 根据ID查询信息
     *
     * @param id 主键
     * @return 单条数据
     */
    public ResponseData findById(Long id) {
        EssayType essayType = essayTypeMapper.selectById(id);
        if(essayType == null || essayType.getId() == null){
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(),essayType);
    }
    
    /**
     * 新增数据
     *
     * @param  essayType 实体
     * @return 新增结果
     */
    public ResponseData add(EssayType essayType) {
        //插入
        if (essayTypeMapper.insert(essayType)>0){
            // 插入成功后要赋值编码
            CodeGenerateStrategy codeGenerateStrategy = codeGenerateStrategyFactory.getCodeGenerateStrategy(CodeGenerateType.ESSAY_TYPE);
            String codePrefix = essayType.getParent().equals("-1") ? "" : essayType.getParent();
            essayType.setCode(codeGenerateStrategy.generateCode(codePrefix, essayType.getId().intValue()));
            essayTypeMapper.updateById(essayType);
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "新增失败，请稍后再试");
    }
    
    /**
     * 更新数据
     *
     * @param essayType 实体
     * @return 更新结果
     */
    public ResponseData update(EssayType essayType) {
        // 更新的时候需要关注code的更新，这里默认都重新赋值
        CodeGenerateStrategy codeGenerateStrategy = codeGenerateStrategyFactory.getCodeGenerateStrategy(CodeGenerateType.ESSAY_TYPE);
        String codePrefix = essayType.getParent().equals("-1") ? "" : essayType.getParent();
        essayType.setCode(codeGenerateStrategy.generateCode(codePrefix, essayType.getId().intValue()));
        //更新
        if (essayTypeMapper.updateById(essayType)>0){
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "修改失败，请稍后再试");
    }
    
    /**
     * 删除数据
     *
     * @param id 主键
     * @return 
     */
    public ResponseData delete(Long id) {
        if (essayTypeMapper.deleteById(id)>0){
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }
    
    /**
     * 批量删除
     * @param idList
     * @return
     */
    public ResponseData deleteBatch(List<Long> idList){
        if (essayTypeMapper.deleteBatchIds(idList)>0){
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }

    /**
     * 树形缩进选项列表查询
     * @param parent
     * @param deleted
     * @return
     */
    public ResponseData getIndentOptions(String parent, Integer deleted) {
        // 查询
        List<TreeOptions> treeOptions = essayTypeMapper.selectTreeOptions(parent, deleted);
        // 整理成带缩进的列表
        SelectOptionsList optionsList = new SelectOptionsList();
        optionsList.makeIndentOptions(treeOptions);
        List<Map> menuOptions = optionsList.getIndentOptionsList();
        if (menuOptions == null || menuOptions.isEmpty()) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), menuOptions);
    }

    /**
     * 树形表格查询
     * @param essayType
     * @return
     */
    public ResponseData getTreeTable(EssayType essayType) {
        // 重新构建查询参数
        QueryEssayType queryEassyType = new QueryEssayType();
        queryEassyType.init(essayType);
        // 查询
        List<TreeEssayTypeTable> treeMenuTables = essayTypeMapper.selectTreeEssayTypeTable(queryEassyType);
        if (treeMenuTables == null || treeMenuTables.isEmpty()) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), treeMenuTables);
    }

    /**
     * 树形选项列表查询
     * @param parent
     * @param deleted
     * @return
     */
    public ResponseData getTreeOptions(String parent, Integer deleted) {
        // 查询
        List<TreeOptions> treeOptions = essayTypeMapper.selectTreeOptions(parent, deleted);
        if (treeOptions == null || treeOptions.isEmpty()) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), treeOptions);
    }

    /**
     * 普通选项列表查询
     * @param deleted
     * @return
     */
    public ResponseData getAllOptions(Integer deleted) {
        LambdaQueryWrapper<EssayType> lqw = new LambdaQueryWrapper<>();
        lqw.eq(!Arrays.asList(null, -1).contains(deleted), EssayType::getDeleted, deleted);
        lqw.select(EssayType::getDeleted, EssayType::getCode, EssayType::getTitle);
        // 查询
        List<Map<String, Object>> normalOptions = essayTypeMapper.selectMaps(lqw);
        if (normalOptions == null || normalOptions.isEmpty()) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), normalOptions);
    }
}
